<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    var templateStr = '<h1>我买了一个{{thing}}，好{{mood}}</h1>';

    var data = {
      thing: '华为手机',
      mood: '开心'
    }
    /**
    * a：匹配到的部分
    * b: 捕获到的部分
    * c: 位置
    * d: 原字符串
    * **/
    // console.log(templateStr.replace(/\{\{(\w+)\}\}/g, function(a,b,c){
    //   console.log(a, b, c);
    //   return '*';
    // }));
    function render(templateStr, data){
      return templateStr.replace(/\{\{(\w+)\}\}/g, function(a,b,c){
        return data[b];
      })
    }

    var result = render(templateStr, data);
    console.log(result);
  </script>
</body>
</html>